package com.woniuxy.aliyunoss.controller;

import com.woniuxy.commons.entity.Email;
import com.woniuxy.commons.utils.ResStatus;
import com.woniuxy.commons.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/aliyunoss/email")
public class EmailController {
    @Resource
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @PostMapping("/")
    public ResponseResult send(@RequestBody Email email) {
        System.out.println("email:"+email);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(email.getTo());
        message.setSubject(email.getTitle());
        message.setText(email.getBody());
        mailSender.send(message);

        return new ResponseResult(200,"发送成功",null, ResStatus.SUCCESS);
    }
}